package com.book.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.book.bean.Book;
import com.book.bean.Messagess;
import com.book.bean.PageVo;
import com.book.bean.Reply;
import com.book.biz.MessageBiz;
import com.book.biz.impl.MessageBizImpl;
import com.book.dao.impl.MessageDaoImpl;


public class SelectMessageServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String type = request.getParameter("type");
		
		MessageBiz msgBiz = new MessageBizImpl();
			if("all".equals(type)){
				//定义页码
				int pageIndex=1;
				//每页显示4个
				int pageSize=5;
				String page=request.getParameter("page");
				if(page!=null && !"".equals(page)) 
					pageIndex=Integer.valueOf(page);
				
				PageVo<Messagess> pv = msgBiz.listAllMsg(pageIndex, pageSize);
				int count =0;
				try {
					count =new MessageDaoImpl().getCount();
				} catch (Exception e) {
					e.printStackTrace();
				}
				request.setAttribute("pv", pv);
				request.setAttribute("count", count);
				request.getRequestDispatcher("message_all.jsp").forward(request, response);
			}
			//我的留言 根据编号
			if("my".equals(type)){
				String uname = (String)request.getSession().getAttribute("uname");
				if(uname==null){
					request.getRequestDispatcher("login_user.jsp").forward(request, response);
				}else{
				//定义页码
				int pageIndex=1;
				//每页显示4个
				int pageSize=4;
				String page=request.getParameter("page");
				if(page!=null && !"".equals(page)) 
					pageIndex=Integer.valueOf(page);
				
				PageVo<Messagess> pv = msgBiz.listMyMsg(pageIndex, pageSize, uname);
				int count =0;
				try {
					count =new MessageDaoImpl().getCount();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				request.setAttribute("pv", pv);
				request.setAttribute("count", count);
				request.getRequestDispatcher("message_my.jsp").forward(request, response);
				}
			}
		
	}

}
